<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="1000px" destroyOnClose>
    <Image v-model:objData="currentData" ref="imageRef" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from "vue"
  import { BasicModal, useModalInner } from "/@/components/Modal"
  import Image from "./Image.vue"

  import { useMessage } from "/@/hooks/web/useMessage"
  const { createMessage } = useMessage()
  // 声明Emits
  const emit = defineEmits(["register", "update:objData"])
  const record = ref<any>({})
  const imageRef = ref()

  const props = defineProps({
    objData: {
      type: Object, // 设置为 Object 的原因，方便属性的传递
      required: true,
    },
  })

  const currentData = computed({
    get: () => props.objData || {},
    set: (val) => {
      emit("update:objData", val)
    },
  })

  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    record.value = data.record
  })
  //设置标题
  const title = computed(() => "素材库")
  //表单提交事件
  async function handleSubmit(v) {
    try {
      setModalProps({ confirmLoading: true })
      imageRef.value?.handleSelectedRow()

      console.log("objData: ", props.objData)

      // await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal()
      createMessage.success(`${unref(title)}成功`)
    } finally {
      setModalProps({ confirmLoading: false })
    }
  }
</script>
